import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 *
 * @author su.gd
 * @date 2023-11-17
 */
public class 指定文件的复制 {

    public static void main(String[] args) throws IOException {
        String projectPath = System.getProperty("user.dir");
        String targetProjectPath = projectPath + File.separator + "code-generator-demo-projects";
        specifyFileCopy("MainTemplate.java", null, targetProjectPath);
    }

    /**
     * 指定文件的复制
     * @param fileName 待复制的文件
     * @param destPath 复制目标位置
     * @param targetProjectPath 复制源位置
     * @throws IOException
     */
    public static void specifyFileCopy(String fileName, String destPath, String targetProjectPath) throws IOException {
        String projectPath = System.getProperty("user.dir");
        if (StringUtils.isBlank(fileName)) {
            System.out.println("文件名不能为空");
            return;
        }
        if (StringUtils.isBlank(destPath)) {
            destPath = projectPath;
        }
        if (StringUtils.isBlank(targetProjectPath)) {
            System.out.println("目标项目路径不能为空");
            return;
        }

        File targetFile = new File(targetProjectPath);
        File[] files = targetFile.listFiles();
        if (files == null || files.length == 0) {
            System.out.println("目标项目路径下没有文件");
            return;
        }

        for (File file : files) {
            if (file.isFile()) {
                if (fileName.equals(file.getName())) {
                    Files.copy(file.toPath(), new File(destPath + File.separator + fileName).toPath(), StandardCopyOption.REPLACE_EXISTING);
                    System.out.println("文件：" + file.getName() + " 复制成功");
                    return;
                }
            } else {
                specifyFileCopy(fileName, destPath, targetProjectPath + File.separator + file.getName());
            }
        }
    }
}
